﻿using Authentication.Data.Repository;
using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract.Authentication.Identifier;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace Authentication.Provider
{
    public class GroupRoleProvider : GenericProvider, IGroupRoleProvider
    {
        public bool Update(ApplicationGroupRule obj)
        {
            try
            {
                var appKey = obj.ApplicationKey.ToUpper();
                var GroupId = obj.GroupId;
                var ObjectId = obj.ObjectId;
                var RuleId = obj.RuleId;
                var GroupChecked = repository.FindOne<ApplicationGroupRule>(x => x.GroupId == GroupId &&
                                                                                 x.ApplicationKey.ToUpper() == appKey &&
                                                                                 x.ObjectId == ObjectId &&
                                                                                 x.RuleId == RuleId);
                if (GroupChecked == null)
                    repository.GroupRoles.Add(obj);
                else
                    GroupChecked.Active = obj.Active;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public IEnumerable<ApplicationGroupRule> RetrieveGroupRules(Guid applicationKey, string groupIdentifier = "")
        {
            var appkey = applicationKey.ToString().ToUpper();
            IEnumerable<ApplicationGroupRule> result = repository.GetQuery<ApplicationGroupRule>(x => x.ApplicationKey.ToUpper() == appkey).Include(x => x.Object);
            if (!string.IsNullOrEmpty(groupIdentifier))
                result = result.Where(x => x.GroupId.ToUpper() == groupIdentifier.ToUpper()).ToList();

            return result.ToList();
        }

        public ApplicationGroupRule Retrieve(Guid applicationKey, string groupid, string objectid, string roleidentifier)
        {
            int ruleId = -1;
            if (RoleIdentifier.IsValid(roleidentifier))
                ruleId = new RoleIdentifier(roleidentifier).PersistableID;

            var appKey = applicationKey.ToString().ToUpper();
            var GroupChecked = repository.GetQuery<ApplicationGroupRule>(x => x.GroupId == groupid &&
                                                                                x.ApplicationKey.ToUpper() == appKey &&
                                                                                x.ObjectId == objectid &&
                                                                                x.RuleId == ruleId).Include(x => x.Object);
            return GroupChecked.FirstOrDefault();

        }

        public bool Delete(ApplicationGroupRule grouprole)
        {
            var appKey = grouprole.ApplicationKey.ToUpper();
            var GroupId = grouprole.GroupId;
            var ObjectId = grouprole.ObjectId;
            var RuleId = grouprole.RuleId;
            var QyeryChecked = repository.GetQuery<ApplicationGroupRule>(x => x.GroupId == GroupId &&
                                                                                x.ApplicationKey.ToUpper() == appKey &&
                                                                                x.ObjectId == ObjectId &&
                                                                                x.RuleId == RuleId).Include(x => x.Object);
            var GroupChecked = QyeryChecked.FirstOrDefault();
            if (GroupChecked == null)
                return false;

            repository.GroupRoles.Remove(GroupChecked);
            repository.UnitOfWork.SaveChanges();
            return true;
        }
    }
}
